package com.herui.sdyu_lib.aliyun.oss;

import android.util.Log;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.herui.sdyu_lib.http.callback.Callback;


/**
 * Created by amanda.shan on 2016/8/2.
 */
public class OssCallBack {

    /**
     * 进度
     */
    public static class ProgressCallBack implements OSSProgressCallback<PutObjectRequest> {

        private Callback.ProgressUpCallback<Void> callback;

        public ProgressCallBack(Callback.ProgressUpCallback<Void> callback) {
            this.callback = callback;
        }

        @Override
        public void onProgress(PutObjectRequest putObjectRequest, long currentSize, long totalSize) {
            if (callback != null)
                callback.onUpLoading(totalSize, currentSize, currentSize == totalSize);
        }
    }

    /**
     * 结束回调
     */
    public static class CompletedCallback implements OSSCompletedCallback<PutObjectRequest,
            PutObjectResult> {

        private Callback.CommonCallback<Void> callback;

        public CompletedCallback(Callback.CommonCallback<Void> callback) {
            this.callback = callback;
        }

        @Override
        public void onSuccess(PutObjectRequest request, PutObjectResult result) {
            Log.d("PutObject", "UploadSuccess");

            callback.onSuccess(null);
        }

        @Override
        public void onFailure(PutObjectRequest request, ClientException clientExcepion,
                              ServiceException serviceException) {
            // 请求异常
            if (clientExcepion != null) {
                // 本地异常如网络异常等
//                clientExcepion.printStackTrace();
                callback.onError(clientExcepion, null, null);
            }
            if (serviceException != null) {
                // 服务异常
//                Log.e("ErrorCode", serviceException.getErrorCode());
//                Log.e("RequestId", serviceException.getRequestId());
//                Log.e("HostId", serviceException.getHostId());
//                Log.e("RawMessage", serviceException.getRawMessage());

                callback.onError(serviceException, serviceException.getErrorCode(),
                        serviceException.getRawMessage());
            }
        }
    }
}
